<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>修改密码--layui后台管理模板</title>
	<meta name="renderer" content="webkit">

	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/layui-admin/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="/layui-admin/css/user.css" media="all" />
	<script src="/js/jquery-1.9.0.min.js"></script>
	<link rel="stylesheet" href="/layui-admin/css/simpleAlert.css">
	<script src="/layui-admin/js/simpleAlert.js"></script>
	<script type="text/javascript" src="/layui-admin/layui/layui.js"></script>
	<script type="text/javascript" src="/layui-admin/page/user/address.js"></script>
	<script type="text/javascript" src="/layui-admin/page/user/user.js"></script>
</head>
<body class="childrenBody">
	<form class="layui-form changePwd">
		<div class="layui-form-item">
		    <label class="layui-form-label">旧密码</label>
		    <div class="layui-input-block">
		    	<input type="password" value="" id="oldPassword"  placeholder="请输入旧密码" lay-verify="required|oldPwd" class="layui-input pwd">
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">新密码</label>
		    <div class="layui-input-block">
		    	<input type="password" value=""  placeholder="请输入新密码" lay-verify="required|newPwd" id="oldPwd" class="layui-input pwd">
		    </div>
		</div>
		<div class="layui-form-item">
		    <label class="layui-form-label">确认密码</label>
		    <div class="layui-input-block">
		    	<input type="password" value="" placeholder="请确认密码" lay-verify="required|confirmPwd" class="layui-input pwd">
		    </div>
		</div>
		<div class="layui-form-item">
		    <div class="layui-input-block">
		    	<button class="layui-btn" lay-submit="" lay-filter="changePassword">立即修改</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</div>
	</form>
<script>
    layui.config({
        base : "js/"
    }).use(['form','layer','jquery','layedit','laydate'],function(){
        var form = layui.form(),
            layer = parent.layer === undefined ? layui.layer : parent.layer,
            laypage = layui.laypage,
            layedit = layui.layedit,
            laydate = layui.laydate,
            $ = layui.jquery;
        layui.upload({
            url: "../../json/userface.json",
            success: function (res) {
                var num = parseInt(4 * Math.random());  //生成0-4的随机数
                //随机显示一个头像信息
                userFace.src = res.data[num].src;
                window.sessionStorage.setItem('userFace', res.data[num].src);
            }
        });
        //添加验证规则
        form.verify({
            newPwd: function (value, item) {
                if (value.length < 6) {
                    return "密码长度不能小于6位";
                }
            },
            confirmPwd: function (value, item) {
                if (!new RegExp($("#oldPwd").val()).test(value)) {
                    return "两次输入密码不一致，请重新输入！";
                }
            }
        })
        form.on('submit(changePassword)', function (data) {
            var user  = new FormData();
            user.append("oldPassword",$("#oldPassword").val());
            user.append("newPassword",$("#oldPwd").val())
            $.ajax({
                url: "http://localhost:8080/user/alter_user_password",
                data: user,
                type: "post",
                dataType: "json",
                cache: false,//上传文件无需缓存
                processData: false,//用于对data参数进行序列化处理 这里必须false
                contentType: false, //必须
                success: function (result) {
                    var data  = eval(result);
                    if (data.msg=="修改成功"){
                        layer.msg(data.msg);
                    }else{
                        layer.msg("密码修改失败！");
                    }
                },
                error: function () {
                    layer.msg("网络发生错误了")
                },
                beforeSend:function(){
                    //弹出loading
                    var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                    setTimeout(function(){
                        top.layer.close(index);
                        layer.closeAll("iframe");
                        //刷新父页面
                        parent.location.reload();
                    },2000);
                }
            });
            return false;//只此一句
        });

    });
</script>

</body>
</html>